home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6352 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: returning ptr to struct with ptrs in it?
  5. Date: 23 Feb 1996 17:30:13 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb23103013@qcd.lanl.gov>
  8. References: <4gk852INNbp4@faatcrl.faa.gov>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: lbona@saratoga's message of 23 Feb 1996 11:24:18 GMT
  13.  
  14. <snip>
  15. In article <4gk852INNbp4@faatcrl.faa.gov>
  16. lbona@saratoga (lbona) writes:
  17.  
  18. <snip>
  19. l: typedef struct bar {
  20. l:   long a;
  21. l:   long b;
  22. l:   int c;
  23. l:   AAA *d;
  24. l:   char e;
  25. l:   char f[100];
  26. l:   AAA *g;
  27. l:   AAA *h;
  28. l: } BAR;
  29. l: 
  30. l: BAR *parse_bar(char toke[]);
  31. l: 
  32. l: main()
  33. l: {
  34. l: BAR BB;
  35. l: char toke[128];
  36. l: 
  37. l:   memset(&BB,0,sizeof(BAR));
  38. l:    /* at this point BB.d == BB.g == BB.h == NULL */
  39.  
  40. Wrong!!!
  41.  
  42. memset cannot be used to set pointers to zero portably. You can
  43. however initialize 
  44.  
  45.    BAR BB = {0};
  46.  
  47. and get what you want.
  48.  
  49. <snip>
  50. l: BAR *parse_bar(char toke[]);
  51. l: {
  52. l: BAR bb;
  53. l: 
  54. l:   memset(&bb,0,sizeof(BAR));
  55. l:    /* at this point BB.d == BB.g == BB.h == NULL */
  56.  
  57. Same comment as before.
  58.  
  59. l: 
  60. l:   /* read data from file and parse - never touch bb.d, bb.g, or bb.h */
  61. l: 
  62. l:    /* at this point BB.d == BB.g == BB.h == NULL */
  63. l:   return(&bb);
  64.  
  65. This is your major problem. Ask yourself: what is bb? bb is a variable
  66. declared within parse_bar, and such variables (unless they are
  67. declared static) disappear when you return from the routine. &bb is a
  68. pointer to that ... but if bb disappears, what good is a pointer to
  69. it? Any attempt to use it outside naturally leads to disasters!
  70. Exactly what form the disaster takes varies from machine to machine:
  71. it may be best not even to think about it.
  72.  
  73. Note that you _can_ return `bb' (if your function had the appropriate
  74. type). It is the _object_ bb that gets destroyed once you leave the
  75. scope of the routine (and hence pointers to it become invalid), the
  76. value can certainly be returned with no problem, if desired.
  77.  
  78. Cheers
  79. Tanmoy
  80. --
  81. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  82. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  83. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  84. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  85. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  86. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  87.